package scau.erps.service.impl;

import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.service.IdEntityService;

import scau.erps.entity.Loan;
import scau.erps.entity.Material;
import scau.erps.entity.Sand;
import scau.erps.entity.Store;
import scau.erps.service.MaterialService;

/** 
 * 原材料服务实现类
 * @author  outsiders 
 *  
 */
public class MaterialServiceImpl extends IdEntityService<Material> implements MaterialService{

	/** 
	 * 申请原材料订单 
	 *
	*/
	public void applyMaterialOrder(Material material) {
		this.dao().insert(material);
		
	}
	/** 
	 * 返回所有原材料订单
	 *  
	*/
	public List<Material> getAllMaterialOrder() {
		return this.dao().query(Material.class,null);
	}

	/** 
	 *  返回当前小组当前时间需入库订单	 *  
	 */
	public List<Material> getGetMaterialOrder(int team_Id, String getTime) {
		SqlExpressionGroup e1 = null;
		SqlExpressionGroup e2 = null;
		e1=Cnd.exps("team_Id", "=", team_Id);
		e2=Cnd.exps("getTime", "=", getTime);
		return this.query(Cnd.where(e1).and(e2), null);
	}
	/** 
	 * 返回当前小组当前时间需付款订单
	 * 
	 */
	public List<Material> getPayMaterialOrder(int team_Id, String payTime) {
		SqlExpressionGroup e1 = null;
		SqlExpressionGroup e2 = null;
		SqlExpressionGroup e3 = null;
		e1=Cnd.exps("team_Id", "=", team_Id);
		e2=Cnd.exps("payTime", "=", payTime);
		
		
		return this.query(Cnd.where(e1).and(e2).and(e3), null);
	}

	/** 	
	 * 对该小组的仓库数据进行更新
	 * 	
	*/
	public void updateStore(int team_Id, String time) {
		List<Material> list=getGetMaterialOrder(team_Id,time);
		if (list.size()>0){
			Store store=this.dao().fetch(Store.class,team_Id);
			Material material=null;
			for (int i=0;i<list.size();i++)
			{
				material=list.get(i);				
				String type=material.getType();
				if (type.equals("M1")) store.setM1(store.getM1()+material.getNumber());
				if (type.equals("M2")) store.setM2(store.getM2()+material.getNumber());
				if (type.equals("M3")) store.setM3(store.getM3()+material.getNumber());
				if (type.equals("M4")) store.setM4(store.getM4()+material.getNumber());
				material.setStatus("已入库");
				this.dao().update(material);
			
			}
			this.dao().update(store);
		}

	}
	/** 
	 * 对需付款订单进行付款操作
	 * 	 
	*/
	public boolean payMaterialOrder(int team_Id, String time) {
		List<Material> list=getPayMaterialOrder(team_Id,time);
		if (list.size()>0){
		  Sand sand=this.dao().fetch(Sand.class, team_Id);
		  int cash = sand.getCash();
		  int pay = 0;
		  for (int i=0;i<list.size();i++)
		  {
			  pay=pay+list.get(i).getNumber();
		  }
		  if (pay>cash) return false;
		  cash=cash-pay;
		  sand.setCash(cash);
		  this.dao().update(sand);
		  Material material=null;
		  for (int i=0;i<list.size();i++)
		  {
			  material=list.get(i);
			  material.setStatus("已付款");
			  this.dao().update(material);
		  }
	}
	return true;
	}
	
	/** 
	 * @author  FYJ
	 * 根据某个要素查找指定用户
	*/
	public List<Material> getMaterial(int team_Id, String type, String status) {
		SqlExpressionGroup e1 = null;
		SqlExpressionGroup e2 = null;
		SqlExpressionGroup e3 = null;
		if(team_Id != 0){
			e1 = Cnd.exps("team_Id", "=", team_Id);			
		}
		if(type != null){
			e2 = Cnd.exps("type", "=", type);
		}
		if(status != null){
			e3 = Cnd.exps("status", "=", status);
		}
		return this.query(Cnd.where(e1).and(e2).and(e3), null);
	}
	@Override
	/** 
	 * @author  FYJ
	 * 根据id找指定用户
	*/
	public Material getMaterialById(int materialId) {
		return this.fetch(materialId);
	}
	@Override
	public void updateMaterial(Material material) {
		this.dao().update(material);
	}
}
